//----------instrument_tektronix.cc----------//

#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
#include <time.h>
#include <math.h>
#include "/usr/local/include/ni488.h"
#include <vector>

#include "instrument_tektronix.hh"

using namespace std;

void instrument_tektronix::instrument_tektronix()
{
	channels = new int[num_channels];
}


void instrument_tektronix::~instrument_tektronix()
{
	delete channels[];
}


void instrument_tektronix::load_config(void)
{
	this->config.load_general_config();	
	this->config.load_specific_config();	
}


void instrument_tektronix::acquire_data(void)
{
	ofstream * headerfile = new ofstream[num_channels];
	ofstream * tracefile = new ofstream[num_channels];
	
	char channel_char[10];
	string channel_filename[50];
	string header_filename[50];
	string trace_filename[50];	

	string my_output_type = this->config.get_output_type();

	for(int channel=0; channel<num_channels; channel++)
	{
		sprintf(channel_char, "%i", channel);
		channel_filename = config.get_base_filename() + channel_char;
		header_filename = channel_filename + "header.txt";
		trace_filename = channel_filename + "traces.dat";
		headerfile[channel]->open(header_filename);
		tracefile[channel]->open(trace_filename);
	}
	
	this->initialize_device(config.get_output_type());
	this->get_scope_parameters();
	this->write_header();

	//START ACQUISITION LOOP-----------------------------------------
	int tracesread = 0;
	fstream tracefile(tracefilename.c_str(), fstream::out);
	cout << "TRACES REQUESTED: " << TracesRequested << endl;
	cout << "MAX RUN TIME: " << MaxRunTime << " seconds" << endl;
	time_t RunTimeStart = time(NULL);
	time_t RunTime = time(NULL);

	while (tracesread<config.get_num_traces_requested()*config.get_num_channels_used() && MaxRunTime > difftime(RunTime, RunTimeStart))
	{
		this->start_aquistion();
		this->wait_for_return();
		
		if(data_type.compare(binary)==0)
		{
			this->acquire_binary();
		}
	
		else if(data_type.compare(ascii)==0) 
		{
			this->acquire_ascii();
		}

		this->restore_operation();

		RunTime = time(NULL);
	    cout << "STORED " << tracesread/NumChannels << " OF " << TracesRequested << 
		" TRACES. TIME ELASPED " << difftime(RunTime, RunTimeStart) << " SECONDS." << endl;
	}    

	tracefile.close();
	cout << "TOTAL TRACES STORED: " << tracesread/NumChannels << endl;

}

/*
void output_help() {
  cout << endl << endl;
  cout << "Run Program with the following: " << endl;
  cout << "./teksoft [filename] ch=[channels] [traces] [fastframe] [maxtime]" << endl;
  cout << " [filename]: this is the base of the filename used for the output.  (e.g. 'mydata' will produce files mydata_traces.bin and mydata_header.txt)" << endl; 
  cout << " [channels]: Channels to be read (e.g. 'ch=2' reads channels 1,2 'ch=4' reads channels 1,2,3,4)" << endl;
  cout << " [traces]: Number of traces to be stored (e.g. '10000' takes 10000 traces)" << endl;
  cout << " [fastframe]: Number of traces to be stored in scope buffer before writing to disk." << endl;
  cout << " [maxtime]: Time in seconds after which the program will terminate." << endl << endl;
  cout << "example line: $./teksoft mydata ch=3 10000 200 3600" << endl;
  cout << endl;

}
*/

//==========specific_config_class==========// 
void instrument_tektronix::specific_config_class::load_specific_config()
{
	ifstream config_file;
	config_file.open(specific_config_filename);

	string value_name;
	config_file >> value_name >> max_run_time;
	config_file >> value_name >> fastframe_numframes;

	config_file.close();
} //end load_specific_config


void instrument_tektronix::specific_config_class::set_MaxRunTime(int MaxRunTime_arg);
{
	MaxRunTime=MaxRunTime_arg;	
}

int instrument_tektronix::specific_config_class::get_MaxRunTime(void);
{
	return MaxRunTime;
}


void instrument_tektronix::specific_config_class::set_fastframe_numframes(int fastframe_numframes_arg);
{
	fastframe_numframes=fastframe_numframes_arg;	
}

int instrument_tektronix::specific_config_class::get_fastframe_numframes(void);
{
	return fastframe_numframes;
}
//=========================================// 

void instrument_tektronix::initialize_device(string data_type)
{
	int dmm;
	int err;
	char buffer[50];
	char  writebuf[50];
	string command;

	//INITIALIZE DEVICE-------------------
	dmm = ibdev(0, 1, 0, 13, 1, 0);
	ibclr(dmm);
	ibtmo( dmm, T3s );
	sprintf(writebuf, "*IDN?");
	ibwrt(dmm, writebuf, 5);
	ibrd(dmm, buffer, 46);  
	cout << "OPENED DEVICE: " << buffer << endl << endl;;
	cout << "---SETTINGS---" << endl;

	//SET SCOPE TO SINGLE ACQUIRE MODE-----
	sprintf(writebuf, "ACQ:STOPAfter SEQUENCE");
	ibwrt(dmm, writebuf, 22); 
	sprintf(writebuf, "ACQ:STOPAfter?");
	ibwrt(dmm, writebuf, 14);
	ibrd(dmm, buffer, 50);
	cout << "STOP AFTER: " << buffer << endl;

	//SET OUPUT FORMAT   --------------------------- 
	for (int i=0; i<NumChannels; i++)
	{  
		command = "DATa:SOURCE CH";
		sprintf(writebuf, "%s%d", command.c_str(), i+1);
		ibwrt(dmm, writebuf, command.length());
	  
		command = "DATa:WIDth 2";
		sprintf(writebuf, "%s", command.c_str());
		ibwrt(dmm, writebuf, command.length());
	  
		if(data_type.compare(binary)==0)
		{
			command = "DATa:ENCdg RIBinary"; //BINARY
		}
		else if(data_type.compare(ascii)==0) 
		{
			command = "DATa:ENCdg ASCIi"; //ASCII
		}

		sprintf(writebuf, command.c_str());
		ibwrt(dmm, writebuf, command.length());
	}

	cout << data_type << endl;

	//SET UP FAST FRAME--------------------
	int ffstate = 0;
	command = "HOR:FAST:STATE?";
	sprintf(writebuf, command.c_str());
	ibwrt(dmm, writebuf, command.length());
	ibrd(dmm, buffer, 1);
	sscanf(buffer, "%d", &ffstate);

	if (ffstate==0) 
	{
		cout << "FAST FRAME MODE ALREADY OFF." << endl;
	}
	else if (ffstate==1)
	{
		cout << "FAST FRAME MODE ALREADY ON. TURNING OFF TO RESET." << endl;
		command = "HOR:FAST:STATE 0";
		sprintf(writebuf, command.c_str());
		ibwrt(dmm, writebuf, command.length());
	}
	
	
	//SET FAST FRAME COUNTS (does not seem to be effective)------
	char ff_ch[10];
	string ff_num_str;
	sprintf(ff_ch, "%d", fastframe_numframes);
	ff_num_str = ff_ch;

	command = "HORizontal:FASTframe:COUNT " + ff_num_str;
	sprintf(writebuf, "%s", command.c_str());
	ibwrt(dmm, writebuf, command.length());
	cout << "ACTIVATE FAST FRAME (" << writebuf << ")" << ff_num_str << " " << ff_ch << endl;
	ibwait(dmm,TIMO|RQS);    

	command = "HOR:FAST:STATE ON";
	sprintf(writebuf, command.c_str());
	ibwrt(dmm, writebuf, command.length());
	ibwait(dmm,TIMO|RQS);

	command = "HORizontal:FASTframe:COUNT?";
	sprintf(writebuf, "%s", command.c_str());
	ibwrt(dmm, writebuf, command.length());
	ibwait(dmm,TIMO|RQS);
	ibrd(dmm, buffer, 10);
} //end initialize_device


void instrument_tektronix::get_scope_parameters(void)
{
	//RETRIEVE SCOPE PARAMETERS--------------------------
	float Parameters[4][7];
	for (int i=0; i<NumChannels; i++) 
	{
	    //Set Channel
	    char channelinput[30];
	    sprintf(channelinput, "data:source CH%d", i+1);
	    command = channelinput;
	    cout << "GETTING PARAMETERS FOR CH " << i+1 << ": " << command << endl;
	    sprintf(writebuf, "%s", channelinput);
	    ibwrt(dmm, writebuf, command.length());
	    
	    //Voltage Multiplier
	    command = "WFMPre:YMUlt?";
	    sprintf(writebuf, command.c_str());
	    ibwrt(dmm, writebuf, command.length());
	    ibrd(dmm, buffer, 20);
	    sscanf(buffer, "%e", &Parameters[i][0]);

	    //Offset Voltage
	    command = "WFMPre:YOFf?";
	    sprintf(writebuf, command.c_str());
	    ibwrt(dmm, writebuf, command.length());
	    ibrd(dmm, buffer, 20);
	    sscanf(buffer, "%f", &Parameters[i][1]);

	    //Bits per Point
	    command = "WFMPre:BIT_Nr?";
	    sprintf(writebuf, command.c_str());
	    ibwrt(dmm, writebuf, command.length());
	    ibrd(dmm, buffer, 20);
	    sscanf(buffer, "%f", &Parameters[i][2]);
	    float exponent = Parameters[i][2];
	    Parameters[i][2] = pow(2.0, exponent/2.0);
	    cout << "BIT_NR: " << exponent << " " << Parameters[i][2] << endl;
	    //Parameters[i][2] = 2^(int(Parameters[i][2]));

	    //Total Number of Points per Trace
	    command = "WFMPre:NR_Pt?";
	    sprintf(writebuf, command.c_str());
	    ibwrt(dmm, writebuf, command.length());
	    ibrd(dmm, buffer, 20);
	    sscanf(buffer, "%f", &Parameters[i][3]);    
	
		//test-------//
		cout << "points in trace (from teksoft): " << Parameters[i][3] << endl;
		//-----------//
	
	    //Trigger Offset
	    command = "WFMPre:YZEro?";
	    sprintf(writebuf, command.c_str());
	    ibwrt(dmm, writebuf, command.length());
	    ibrd(dmm, buffer, 20);
	    sscanf(buffer, "%f", &Parameters[i][4]);    

	    //Time Interval per Point
	    command = "WFMOutpre:XINcr?";
	    sprintf(writebuf, command.c_str());
	    ibwrt(dmm, writebuf, command.length());
	    ibrd(dmm, buffer, 20);
	    sscanf(buffer, "%f", &Parameters[i][5]);    

	    //Number of Frames 
	    /*
	    command = "WFMOutpre:NR_FR?";
	    sprintf(writebuf, command.c_str());
	    ibwrt(dmm, writebuf, command.length());*/
	    command = "HORizontal:FASTframe:COUNT?";
	    sprintf(writebuf, "%s", command.c_str());
	    ibwrt(dmm, writebuf, command.length());
	    ibwait(dmm,TIMO|RQS);
	    ibrd(dmm, buffer, 20);
	    ibwait(dmm,TIMO|RQS);
	    sscanf(buffer, "%f", &Parameters[i][6]);
	}
} //end get_scope_parameters


void instrument_tektronix::write_header(void)
{ 
	//OUTPUT HEADERFILE------------------------------
	fstream headerfile(headerfilename.c_str(), fstream::out);
	char headerdata[256];
	for (int i=0; i<

	for (int i=0; i<NumChannels; i++) 
	{
    	//sprintf(headerdata, "%e %e %e %e %e %e %e", Parameters[i][0], Parameters[i][1],Parameters[i][2],Parameters[i][3],Parameters[i][4],Parameters[i][5],Parameters[i][6]);
	    //headerfile << headerdata << endl;
	    cout << "CHANNEL " << i+1 << " PARAMETERS: ";
	    for (int a=0; a<7; a++) 
		{
	    	headerfile << Parameters[i][a] << " ";
			cout << Parameters[i][a] << " ";
    	}
		headerfile << TracesRequested << " "; //This must be added for TraceToRoot's trace reading loop, so it knows when to stop.
    	headerfile << endl;
		cout << endl;
	}
	headerfile.close();
} //end write_header


void instrument_tektronix::start_acquisition(void)
{
	//START ACQUISITION------------------------------
	command = "ACQ:STATE RUN";
	sprintf(writebuf, command.c_str());
	ibwrt(dmm, writebuf, command.length());
} //end start_acquistion

	
void instrument_tektronix::wait_for_return(void)
{
	//WAIT FOR ACQUITION RETURN----------------------
	cout << endl << "===WAITING FOR DATA===" << endl; 
	command = "*OPC?";
	sprintf(writebuf, command.c_str());
	time_t acq_timeout = time(NULL) + 300; //in seconds -> 5 minutes 
	int acq_status = 0;
	
	while (time(NULL) < acq_timeout)
	{
		ibwrt(dmm, writebuf, command.length());
		ibrd(dmm, buffer, 5);
		sscanf(buffer, "%d", &acq_status);
		//FOR DEBUGGING
		//cout << "Polling: " << writebuf << " --> ";
		//cout << "Buffer: " << buffer;
		//cout << " Status: " << acq_status << endl;
		//cout << "ELAPSED: " << time(NULL) << " TIMEOUT: " << acq_timeout << endl;
		if (acq_status==1)
		{
			//cout << "WAVEFORM(S) ACQUIRED" << endl;
			break;
		}
		sleep(1); //Pause for One second
	}

	if (acq_status==0)
	{
		cout << "ACQUISTION TIME OUT" << endl;
		cout << "END PROGRAM" << endl;
		ibonl(dmm, 0);
		return 0;
	}

} //end wait_for_return()

    
void instrument_tektronix::acquire_ascii(void)
{
	//ASCII-----------------------------------  
    char* trace;
    string chstring;
    char ch_char[5];
    for (int ch=0; ch<NumChannels; ch++) {
    
	sprintf(ch_char, "CH%d", ch+1);
	chstring = ch_char;
	//ALLOCATE MEMORY FOR FRAMES
	int frames = int(Parameters[ch][6]);   
	cout << "ALLOCATE MEMORY, FRAMES: " << frames << endl;
	cout << "CHANNEL: " << ch << endl;
	const int numpoints =  int(Parameters[ch][6]*(Parameters[ch][3]*200));
	trace[ch] = new char[ numpoints ];

	//if ((trace[ch] = (char**)malloc((size_t)(frames))) == NULL) {}
	//cout << "OUT OF MEMORY" << endl;
	//return 0;
	//}
	
	//CALL CHANNEL
	cout << "CALL CHANNEL, Buffer: ";
	command = "data:source " + chstring;
	sprintf(writebuf, "%s", command.c_str());
	cout << writebuf << endl;
	ibwrt(dmm, writebuf, command.length());    
	ibwait(dmm, TIMO|RQS);
	command = "DATa:SOUrce?";
	sprintf(writebuf, "%s", command.c_str());
	ibwrt(dmm, writebuf, command.length());
	ibrd(dmm, buffer, 4);
	buffer[4] = '\0';
	cout << "SOURCE: " << buffer << endl;
   
	ibwait(dmm, TIMO|RQS);


	//CALL WAVEFORM
	command = "CURVe?";
	sprintf(writebuf, command.c_str());
	unsigned short command_status = ibwrt(dmm, writebuf, command.length());
	cout << "WAVEFORM CALLED, CHANNEL " << ch+1 << endl;
	//if ( (command_status & (1<<15) )!=0 || (command_status & (1<<14) )!=0) 
	if ( command_status!=2304 && command_status!=10496) {
	    cout << "ERROR from ibwrt CURVe?: " << command_status << endl;
	    for (int bit=0; bit<16; bit++) {
		unsigned short flag = (1<<bit);
		if ( (command_status & flag) != 0 )
		    cout << "  ERROR BIT " << bit+1 << " set" << endl;
		else
		    cout << "  ERROR BIT " << bit+1 << " not set" << endl;
	    }
	    return 0;
	}
	ibwait(dmm, TIMO|RQS);
	cout << "   reading points (parameter 3):" << Parameters[ch][3] << endl;
	cout << "   number of traces (parameter 6):" << Parameters[ch][6] << endl;
	//for (int i=0; i<int(Parameters[ch][6]);i++) {
	//if ((trace[ch][i] = (char*)malloc((size_t)(Parameters[ch][3]*10*3))) == NULL) {
	//cout << "OUT OF MEMORY" << endl;
	//return 0;
	//}
	//ibrd(dmm, trace[ch], numpoints);
	//cout << "READ WAVEFORM " << i << endl;
	    
	command_status = ibrd(dmm, trace[ch], numpoints);
	if ( command_status!=2304 && command_status!=10496 ) {
	    cout << "READ ERROR: command status " << command_status << endl;
	    for (int bit=0; bit<16; bit++) {
		unsigned short flag = (1<<bit);
		if ( (command_status & flag) != 0 )
		    cout << "  ERROR BIT " << bit+1 << " set" << endl;
		else
		    cout << "  ERROR BIT " << bit+1 << " not set" << endl;
	    }
	    cout << "READ ERROR: iberr " << iberr << endl;
	    return 0;
	}

	//tracefile << trace[ch][i] << endl << endl;

	//Now write the data to the output file
	//the conditional is due to the behavior of strtok,
	//it must be run once with the string to be tokenized as an arg,
	//and subsequently with NULL as an argument.
	
	//tracefile << trace[ch] << endl;

	int trace_counter;
	int point_counter;

	for (trace_counter=0; trace_counter<Parameters[ch][6]; trace_counter++)
	{

		if (trace_counter==0)
		{
			tracefile << strtok (trace[ch], ",\n") << ",";
			for(point_counter=0; point_counter<Parameters[ch][3]-1; point_counter++)
			{
				tracefile << strtok(NULL, ",\n") << ","; //Separate the data into traces 
			}
			
		}

		else
		{
			for(point_counter=0; point_counter<Parameters[ch][3]; point_counter++)
			{
				tracefile << strtok(NULL, ",\n") << ","; //Separate the data into traces 
			}
			
		}
	
		tracefile << endl; //At the end of each trace, end the line and add a newline.

	}

	//cout << "WAVEFORM: " << trace[ch][i] << endl;
	tracesread += int(Parameters[ch][6]);

    }//end of loop over channels

    //FREE MEMORY, ASCII----------------------------
    cout << "FREE MEMORY" << endl;
    for (int ch=0; ch<NumChannels;ch++)
	{
		//for (int i=0; i<int(Parameters[ch][6]);i++) {
		//FREE TRACES IN FRAME
		//free(trace[ch][i]);
		cout << "FREE FRAME BUFFER" << endl;
		delete [] trace[ch];
		//}
		//free(trace[ch]);
		cout << "FREE CHANNEL" << endl;
    }
} //end acquire_ascii    
  

void instrument_tektronix::acquire_binary(void)
{
	//READ DATA, BINARY------------------------------------------ 
    char** trace[4];
    char cheattrace[1000];  
    string chstring;
    char ch_char[5];
    int bytestreamlength = 0;

    for (int ch=0; ch<NumChannels; ch++)
	{
		//ALLOCATE MEMORY FOR FRAMES
	    int frames = int(Parameters[ch][6]);   
	    //cout << "ALLOCATE MEMORY, FRAMES: " << frames << endl;

	    trace[ch] = new char*[frames];

	    //GET WAVEFORM------------------------------    
	    //CALL CHANNEL
	    sprintf(ch_char, "CH%d", ch+1);
	    chstring = ch_char;
	    command = "data:source " + chstring;
	    sprintf(writebuf, "%s%d", command.c_str(), ch+1);
	    ibwrt(dmm, writebuf, command.length());    
	    command = "DATa:SOUrce?";
	    sprintf(writebuf, "%s", command.c_str());
	    ibwrt(dmm, writebuf, command.length());
	    ibrd(dmm, buffer, 4);
	    buffer[4] = '\0';
	    // cout << "SOURCE: " << buffer << endl;;
	    ibwait(dmm, TIMO|RQS);
	    //CALL WAVEFORM
	    command = "CURVe?";
	    sprintf(writebuf, command.c_str());
	    ibwrt(dmm, writebuf, command.length());
	    //cout << "WAVEFORM CALLED, CHANNEL " << ch+1 << endl;
	    ibwait(dmm, TIMO|RQS);

	    //READ # SYMBOL
	    char c[2];
	    ibrd(dmm, &c, 1);
	    //cout << c << endl;
	    c[1] = '\0';
	    //READ NUMBER OF BYTES TO BE READ
	    int len;
	    ibrd(dmm, &c, 1);
	    c[1] = '\0';
	    len = atoi(&c[0]); 
	    //cout << c << " "  << len << endl;
	    //READ STRING CONTAINING NUMBER OF BYTES TO TRANSFER
	    char recordl[20];
	    if(ibrd(dmm,recordl,len) & ERR)
		cout << "ERROR, recordl" << endl;
	    recordl[len] = '\0';
	    bytestreamlength = atoi(recordl);
	    //cout << "FINALLY: " << len << " " << bytestreamlength << endl;
	    
	    const int tracepoints = int(bytestreamlength/Parameters[ch][6]);
	    //COLLECT TRACE FOR EACH FRAME
	    for (int i=0; i<int(Parameters[ch][6]);i++)
		{
		
			//ALLOCATING TRACESIZE
			//cout << "ALLOCATING TRACE FOR FRAME " << i << ", " <<  bytestreamlength/Parameters[ch][6] << " points" << endl;
			//trace[ch][i] = (char*)malloc((size_t)(bytestreamlength/Parameters[ch][6]));
			//C STYLE ALLOCATION, FRAMES
	
			//if (trace[ch][i] = (char*)malloc((size_t)(bytestreamlength/Parameters[ch][6])) == NULL) {
			//cout << "OUT OF MEMORY" << endl;
			//return 0;
			//}
	
			trace[ch][i] = new char[tracepoints];
			
			if (ibrd(dmm, trace[ch][i], int(bytestreamlength/Parameters[ch][6])) & ERR)
			  cout << "ERROR, READING TRACE FOR FRAME " << i << endl;
			else
			  tracesread++;
			//if (ibrd(dmm, cheattrace, bytestreamlength+1) & ERR)
			//	cout << "ERROR, trace" << endl;
			cout << "RETRIEVED WAVEFORM, CHANNEL " << ch+1 << " FRAME " << i+1 << ": " << trace[ch][i] << endl;
		}
    

	}
    
    //TRANSLATE, for debugging-----------------------------------
    const int FRAMES = int(Parameters[0][6]);
    const int POINTS = int(Parameters[0][3]);
    /*
    cout << "TRANSLATE" << endl;
    float yzero = Parameters[0][4];
    float point = 0.0;
    int** Data[4];
    for (int ch=0; ch<NumChannels; ch++) {
      //ALLOCATING DATA, FRAME
      //Data[ch] = (int**)malloc((size_t)(Parameters[ch][6])); 
      Data[ch] = new int*[FRAMES];
      cout << "TRANSLATE CHANNEL " << ch+1 << endl;
      for (int i=0; i<int(Parameters[ch][6]);i++) {
	//Data[ch][i] = (int*)malloc((size_t)(Parameters[ch][3]));
	Data[ch][i] = new int[POINTS];
	
	for(int pt=0;pt<bytestreamlength;pt++) {
	  //twob = (int(trace[ch][i][pt]) || int(trace[ch][i][pt+1]));
	  //double twob = ((double)cheattrace[pt] || (double)cheattrace[pt+1]*256);
	  //float onea = float(cheattrace[pt]);
	  //float oneb = float(cheattrace[pt+1])*256;
	  float onea = float(trace[ch][i][pt]);
	  float oneb = float(trace[ch][i][pt+1]);
	  Data[ch][i][pt/2] = int(onea);
	  //Data[ch][i][pt+1] = int(oneb);
	  //if (onea!=0)
	  //  value=onea;
	  //else
	  //  value=oneb;
	  float value = float(Data[ch][i][pt/2]*256);
	  //if (oneb!=0)
	  //  value=onea*oneb;
	  point = (value-int(Parameters[ch][1]))*Parameters[ch][0] + float(Parameters[ch][4]);
	  cout << (pt)/2 << ": " << point << " " << onea << " " << oneb << " ";	
	  //cout << value << " " << int(Parameters[ch][1]) << " " << Parameters[ch][0] << " " << float(Parameters[ch][4]) << endl;
	  //Data[ch][i][pt] = atof(output);
	  pt++;
	}
	
      }
      cout << endl << endl;
    }
    */

    //STORE IN FILE----------------------------------
 
//     for (int i=0; i<FRAMES;i++) {
//       for (int ch=0;ch<NumChannels;ch++){
// 	for (int pt=0; pt<1000;pt++) {
// 	    //tracefile << char(trace[ch][i][pt]);
// //	  tracefile << trace[ch][i][pt];
// //	
// 	    //cout << trace[ch][i][pt] << endl ;
// //
// 	  pt++;
// 	}	
// 	//cout << "WROTE CHANNEL " << ch+1 << ", FRAME " << i+1 << endl;
//       }	
//     }

    
    
//     //FREE MEMORY, BINARY----------------------------
//     //cout << "FREE MEMORY" << endl;
//     for (int ch=0; ch<NumChannels;ch++) {
//       for (int i=0; i<int(Parameters[ch][6]);i++) {
// 	//FREE TRACES IN FRAME
// 	//delete[] Data[ch][i];
// 	//free(Data[ch][i]);
// 	//free(trace[ch][i]);
// 	delete[] trace[ch][i];
// 	//cout << "FREE FRAME" << endl;
//       }
//       //delete[] Data[ch];
//       //free(Data[ch]);
//       //free(trace[ch]);
//       delete[] trace[ch];
//       //cout << "FREE CHANNEL" << endl;
//     }
} //end acquire_binary


void instrument_tektronix::restore_operation(void)
{
	//RESTORE OPERATION---------------------------
	//TURN FAST FRAME OFF
	command = "HOR:FAST:STATE 0";
	sprintf(writebuf, command.c_str());
	ibwrt(dmm, writebuf, command.length());
	//SET SCOPE ACQ MODE BACK TO RUN
	command = "ACQ:STOPAfter RUNSTop";
	sprintf(writebuf, command.c_str());
	ibwrt(dmm, writebuf, command.length());
	command = "ACQ:STATE ON";
	sprintf(writebuf, command.c_str());
	ibwrt(dmm, writebuf, command.length());
	//DEVICE OFFLINE-----------------------
	ibonl(dmm, 0);
} //end restore_operation 

/* 
  cout << "END OF PROGRAM" << endl;

  return 0;
  
}
*/
//----------instrument_tektronix.cc----------//
